﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IFSModel;
using IFSController;
using System.Web.SessionState;

namespace API
{
    public class ifs : IHttpHandler, IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest post = context.Request;
            string ADAuthen = context.User.Identity.Name;
            string ip = HttpContext.Current.Request.UserHostAddress;
            string Medthod = post["metdhodName"];
            if (!string.IsNullOrEmpty(Medthod))
            {
                switch (Medthod.Trim().ToLower())
                {
                    case "addinvitefriend":
                        var inviteData = post["postData"];
                        var invites = JsonConvert.DeserializeObject<List<friendsInvite>>(inviteData);
                        Helper.Result resultInvite;
                        member.AddFriendInvite(invites, out resultInvite);
                        context.Response.ContentType = "text/plain";
                        context.Response.Write(JsonConvert.SerializeObject(resultInvite));
                        context.Response.End();
                        break;
                    case "addlive":
                        var data = post["postData"];
                        var scores = JsonConvert.DeserializeObject<List<liveScore>>(data);
                        scores.ForEach(item => { if (item.sj != null) { item.sj = item.sj.Value.AddHours(6); } });
                        score.AddLiveScore(scores);
                        game.UpdateMemberPlay();
                        
                        context.Response.ContentType = "text/plain";
                        context.Response.End();
                        break;
                    case "getleague":

                        string leagueType = post["leagueType"];
                        var league = score.GetLeague(leagueType);
                        context.Response.ContentType = "text/plain";
                        context.Response.Write(league);
                        context.Response.End();
                        break;
                    case "getmatch":
                        var matchDate = post["matchDate"];
                        var matchSince = post["matchSince"];
                        var matchUntil = post["matchUntil"];
                        var leagues = post["leagues"];
                        var result = string.Empty;
                        if (!string.IsNullOrEmpty(matchDate))
                        {
                            var matchDate2 = JsonConvert.DeserializeObject<DateTime>(matchDate);
                            result = score.GetMatch(matchDate2);
                        }
                        else if (!string.IsNullOrEmpty(matchSince) && !string.IsNullOrEmpty(matchUntil))
                        {
                            var matchSince2 = JsonConvert.DeserializeObject<DateTime>(matchSince);
                            var matchUntil2 = JsonConvert.DeserializeObject<DateTime>(matchUntil);
                            result = score.GetMatch(matchSince2, matchUntil2);
                        }
                        else if (!string.IsNullOrEmpty(leagues))
                        {
                            result = score.GetMatch(leagues);
                        }
                        else
                        {
                            result = score.GetMatch();
                        }
                        context.Response.ContentType = "text/plain";
                        context.Response.Write(result);
                        context.Response.End();
                        break;
                    case "getcurrent":
                        context.Response.ContentType = "text/plain";
                        var username = post["username"];
                        if (string.IsNullOrEmpty(username))
                        {
                            context.Response.Write(IFSController.member.GetMember());
                        }
                        else
                        {
                            context.Response.Write(IFSController.member.GetMember(username));
                        }
                       
                        context.Response.End();
                        break;
                    case "getmatchscore":
                        var matchDateScore = post["matchDate"];
                        var matchSinceScore = post["matchSince"];
                        var matchUntilScore = post["matchUntil"];
                        var leaguesScore = post["leagues"];
                        var resultScore = string.Empty;

                        result = score.GetMatchPrices(leaguesScore,
                            matchDateScore,
                            matchSinceScore,
                            matchUntilScore);
                        
                        context.Response.ContentType = "text/plain";
                        context.Response.Write(result);
                        context.Response.End();
                        break;
                    case "testupdatescore":

                        game.UpdateMemberPlay();
                        member.AutoAddStar();
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("done");
                        context.Response.End();
                        break;
                    case "getmemberplayhistory":
                        var historyLeagueID = post["leagueid"];
                        var historySince = post["matchSince"];
                        var historyUntil = post["matchUntil"];
                        var historypage = post["page"];
                        int page = 1;
                        if (!string.IsNullOrEmpty(historypage ))
                        {
                            page = Convert.ToInt32(historypage);
                        }
                        string resutl = member.getMemberPlayHistory(historyLeagueID,
                            historySince,
                            historyUntil,
                            page
                            );
                        context.Response.Write(resutl);
                        context.Response.ContentType = "text/plain";
                        context.Response.End();
                        break;

                }
            }
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}